package com.suyafei.controller;


import com.suyafei.enums.BizCodeEnum;
import com.suyafei.request.AddressAddRequest;
import com.suyafei.util.JsonData;
import com.suyafei.model.AddressDO;
import com.suyafei.service.AddressService;
import com.suyafei.vo.AddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

/**
 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author suyafei
 * @since 2021-01-28
 */
@Api(tags = "收货地址模块", description = "用户CRUD用户地址")
@RestController
@RequestMapping("/api/address/v1/")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 添加收货地址
     *
     * @param addressAddRequest
     * @return
     */
    @ApiOperation("添加收货地址")
    @PostMapping("add_address")
    public JsonData addAddress(@ApiParam("地址对象")
                               @RequestBody AddressAddRequest addressAddRequest) {
        addressService.add(addressAddRequest);
        return JsonData.buildSuccess();
    }

    /**
     * 根据Id查询地址
     *
     * @param addressId
     * @return
     */
    @ApiOperation("RPC-根据Id查找地址详情")
    @GetMapping("/find/{address_id}")
    public JsonData detail(
            @ApiParam(value = "地址id", required = true)
            @PathVariable("address_id") long addressId) {
        AddressVO addressVO = addressService.detail(addressId);
        return addressVO == null ? JsonData.buildResult(BizCodeEnum.ADDRESS_NO_EXITS) : JsonData.buildSuccess(addressVO);
    }

    /**
     * 根据Id删除地址
     *
     * @param addressId
     * @return
     */
    @ApiOperation("根据Id删除地址")
    @GetMapping("/del/{address_id}")
    public JsonData del(
            @ApiParam(value = "地址id", required = true)
            @PathVariable("address_id") long addressId) {
        int rows = addressService.del(addressId);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.ADDRESS_DEL_FAIL);

    }

    @ApiOperation("查询用户所有的收货地址")
    @GetMapping("lsit")
    public JsonData findUserAllAddress() {
        List<AddressVO> addressVOLis = addressService.listUserAllAddress();
        return addressVOLis != null ? JsonData.buildSuccess(addressVOLis) : JsonData.buildSuccess();
    }
}

